package com.base.helper;

import com.base.po.param.Maps;
import net.sf.json.JSONObject;

import javax.jws.Oneway;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

public class BaiduMapHelper {

    private static String ak = "OKEgAxUdPb3Xel1IXMTsKDw0KgN83ZH3"; // 百度地图api ak

    public static Map<String, Object> getCity(HttpServletRequest request, Double longitude, Double latitude) {
        String url = "http://api.map.baidu.com/geocoder/v2/?location=" + longitude + "," + latitude +"&output=json&pois=1&latest_admin=1&ak=" + ak;

        String data = null;
        JSONObject obj = null;
        Maps maps = null;
        Map<String, Object> jsonMap = new HashMap<>();
        try {
            data = WebHelper.GetData(url, null);
            System.out.println(data);
            obj = JSONObject.fromObject(data);
            maps = (Maps) JSONObject.toBean(obj, Maps.class);
            jsonMap.put("country", maps.getResult().getAddressComponent().getCountry());
            jsonMap.put("province", maps.getResult().getAddressComponent().getProvince());
            jsonMap.put("city", maps.getResult().getAddressComponent().getCity());
            jsonMap.put("district", maps.getResult().getAddressComponent().getDistrict());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return jsonMap;
    }
}
